package com.koron.ticket.power;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.koron.bean.base.Response;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.ticket.feign.SystemFeign;
import com.koron.ticket.power.bean.TicketNodeBean;
import com.koron.ticket.power.bean.TicketPowerVo;
import com.koron.ticket.work.workticket.bean.WorkTicket;
import com.koron.ticket.work.workticket.bean.WorkTicketQuery;
import com.koron.ticket.work.workticket.mapper.WorkTicketMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.koron.ticket.work.enums.TicketStatus.*;

@Api(tags = "两票管理-权限")
@RequestMapping("ticket/power")
@Slf4j
@RestController
public class TicketPowerController {

    private EamUser getEamuser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    private SystemFeign systemfeign;

    @Autowired
    private TicketPowerService ticketpowerservice;

    @GetMapping("getPower")
    @ApiOperation("获取权限")
    public Response<TicketPowerVo> getPower(String id) {
        TicketPowerVo power = ADOConnection.runTask(getEamuser().getCurrDs(), ticketpowerservice, "getPower", TicketPowerVo.class, id);
        return Response.ok(power);
    }

    @GetMapping("getPowerNew")
    @ApiOperation("获取权限")
    public Response<TicketNodeBean> getPowerNew(@RequestParam("id") String id,@RequestParam("type") String type,@RequestParam("status") Integer status) {
        TicketNodeBean power = ADOConnection.runTask(getEamuser().getCurrDs(), ticketpowerservice, "getPowerNew", TicketNodeBean.class, id, type, status);
        return Response.ok(power);
    }

//    @GetMapping("getPower")
//    @ApiOperation("获取权限")
//    public Response getPower() {
//        PostUserQuery postUserQuery = new PostUserQuery();
//        postUserQuery.setUserAccount(getEamuser().getAccount());//shanzh  krzhoujun
//        List<PostUserVo> postlist = getPostUserVos(postUserQuery); // 获取人员的岗位信息
//        List<String> postNamelist = postlist.stream().map(PostUserVo::getPostName).collect(Collectors.toList());//员工岗位位集合
//
//        // true权限隐藏、 false权限显示
//        TicketPowerVo ticketPowerVo = new TicketPowerVo();
//
//        List<TicketNodeBean> gasandmechanics = InitData.initGasAndMechanicsNode();
//        setGasAndMechanicsPower(postNamelist, gasandmechanics);
//        ticketPowerVo.setGasAndMechanics(gasandmechanics);// 电气第一第二种、机械工作票集合
//
//        List<TicketNodeBean> third = InitData.initThirdicsNode();
//        setThirdPower(postNamelist, third);
//        ticketPowerVo.setThird(third);// 第三种工作票集合
//
//        List<TicketNodeBean> secondeLevel = InitData.initSecondeLevelNode();
//        setSecondLevel(postNamelist, secondeLevel);
//        ticketPowerVo.setSecondLevel(secondeLevel);// 动火第二种工作票集合
//
//        List<TicketNodeBean> powerLin = InitData.initPowerLinNode();
//        setSecondLevel(postNamelist, powerLin);
//        ticketPowerVo.setPowerLin(powerLin);// 线路第一第二种工作票集合
//
//        return Response.ok(ticketPowerVo);
//    }

    /**
     * 配置线路一二种工作票权限
     *
     * @param postNamelist
     * @param ticketNodeBeans
     */
    private void setPowerLin(List<String> postNamelist, List<TicketNodeBean> ticketNodeBeans) {
        ticketNodeBeans.stream().forEach(n -> {
            // 填写
            if (n.getStatus().equals(WRITE.getStatus())) {
                n.getDet().stream().forEach(p -> p.setIsPower(false));
            }
            // 待签发
            if (n.getStatus().equals(SIGN.getStatus())) {
                if (postNamelist.contains("电力线路工作票签发人")) {
                    n.getDet().stream().forEach(p -> p.setIsPower(false));
                }
            }
            // 待会签
            if (n.getStatus().equals(COUNTERSIGN.getStatus())) {
                if (postNamelist.contains("电力线路工作票签发人") || getEamuser().getIsOutOrg().equals("1")) {
                    n.getDet().stream().forEach(p -> p.setIsPower(false));
                }
            }
            // 待接受
            if (n.getStatus().equals(ACCEPT.getStatus())) {
                if (postNamelist.contains("值班负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "reject")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 待许可
            if (n.getStatus().equals(PERMIT.getStatus())) {
                if (postNamelist.contains("工作许可人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "reject")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 工作待终结
            if (n.getStatus().equals(WORK_END.getStatus())) {
                if (postNamelist.contains("机电工作负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "change")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 工作票待终结
            if (n.getStatus().equals(TICKET_END.getStatus())) {
                if (postNamelist.contains("值班负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 已归档
            if (n.getStatus().equals(ARCHIVED.getStatus())) {
                n.getDet().stream().forEach(p -> {
                    if (!p.getCode().equals("save")) {
                        p.setIsPower(false);
                    }
                });
            }
        });
    }

    /**
     * 配置动火第二种工作票权限
     *
     * @param postNamelist
     * @param secondeLevel
     */
    private void setSecondLevel(List<String> postNamelist, List<TicketNodeBean> secondeLevel) {
        secondeLevel.stream().forEach(n -> {
            // 填写
            if (n.getStatus().equals(WRITE.getStatus())) {
                n.getDet().stream().forEach(p -> p.setIsPower(false));
            }
            // 待签发
            if (n.getStatus().equals(SIGN.getStatus())) {
                if (postNamelist.contains("动火工作票签发人")) {
                    n.getDet().stream().forEach(p -> p.setIsPower(false));
                }
            }
            // 待会签
            if (n.getStatus().equals(COUNTERSIGN.getStatus())) {
                if (postNamelist.contains("动火工作票签发人")) {
                    n.getDet().stream().forEach(p -> p.setIsPower(false));
                }
            }
            // 待接受
            if (n.getStatus().equals(ACCEPT.getStatus())) {
                if (postNamelist.contains("值班负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "reject")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 待许可
            if (n.getStatus().equals(PERMIT.getStatus())) {
                if (postNamelist.contains("工作许可人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "reject")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 工作待终结
            if (n.getStatus().equals(WORK_END.getStatus())) {
                if (postNamelist.contains("机电工作负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 已归档
            if (n.getStatus().equals(ARCHIVED.getStatus())) {
                n.getDet().stream().forEach(p -> {
                    if (!p.getCode().equals("save")) {
                        p.setIsPower(false);
                    }
                });
            }
        });
    }

    /**
     * 配置第三种工作票权限
     *
     * @param postNamelist
     * @param third
     */
    private void setThirdPower(List<String> postNamelist, List<TicketNodeBean> third) {
        third.stream().forEach(n -> {
            // 填写
            if (n.getStatus().equals(WRITE.getStatus())) {
                n.getDet().stream().forEach(p -> p.setIsPower(false));
            }
            // 待许可
            if (n.getStatus().equals(PERMIT.getStatus())) {
                if (postNamelist.contains("工作许可人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "reject", "operationTicket")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 工作票待终结
            if (n.getStatus().equals(TICKET_END.getStatus())) {
                if (postNamelist.contains("工作许可人") || postNamelist.contains("机电工作负责人（第三种）")) {
                    n.getDet().stream().forEach(p -> p.setIsPower(false));
                }
            }
            // 已归档
            if (n.getStatus().equals(ARCHIVED.getStatus())) {
                n.getDet().stream().forEach(p -> {
                    if (!p.getCode().equals("save")) {
                        p.setIsPower(false);
                    }
                });
            }
        });
    }

    /**
     * 配置第一二种工作票、机械工作票权限
     *
     * @param postNamelist
     * @param ticketNodeBeans
     */
    private void setGasAndMechanicsPower(List<String> postNamelist, List<TicketNodeBean> ticketNodeBeans) {
        ticketNodeBeans.stream().forEach(n -> {
            // 填写
            if (n.getStatus().equals(WRITE.getStatus())) {
                n.getDet().stream().forEach(p -> p.setIsPower(false));
            }
            // 待签发
            if (n.getStatus().equals(SIGN.getStatus())) {
                if (postNamelist.contains("机电工作票签发人")) {
                    n.getDet().stream().forEach(p -> p.setIsPower(false));
                }
            }
            // 待会签
            if (n.getStatus().equals(COUNTERSIGN.getStatus())) {
                if (postNamelist.contains("机电工作票签发人")) {
                    n.getDet().stream().forEach(p -> p.setIsPower(false));
                }
            }
            // 待接受
            if (n.getStatus().equals(ACCEPT.getStatus())) {
                if (postNamelist.contains("值班负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "reject")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 待许可
            if (n.getStatus().equals(PERMIT.getStatus())) {
                if (postNamelist.contains("工作许可人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "operationTicket", "reject")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 工作待终结
            if (n.getStatus().equals(WORK_END.getStatus())) {
                if (postNamelist.contains("机电工作负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "change")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 工作票待终结
            if (n.getStatus().equals(TICKET_END.getStatus())) {
                if (postNamelist.contains("值班负责人")) {
                    n.getDet().stream().forEach(p -> {
                        if (StrUtil.equalsAny(p.getCode(), "save", "preview", "submit", "operationTicket")) {
                            p.setIsPower(false);
                        }
                    });
                }
            }
            // 已归档
            if (n.getStatus().equals(ARCHIVED.getStatus())) {
                n.getDet().stream().forEach(p -> {
                    if (!p.getCode().equals("save")) {
                        p.setIsPower(false);
                    }
                });
            }
        });
    }


    @GetMapping("test")
    @ApiOperation("测试")
    public Response test(@RequestParam("id") String id) {
//        PostUserQuery postUserQuery = new PostUserQuery();
//        postUserQuery.setUserAccount("krzhoujun");
//        List<PostUserVo> postlist = getPostUserVos(postUserQuery); // 获取人员的岗位信息
        Map<String, List<String>> map = ADOConnection.runTask(getEamuser().getCurrDs(), factory -> {
            WorkTicket ticketBean = factory.getMapper(WorkTicketMapper.class).getSingle(new WorkTicketQuery(id));
            String ticketUnit = ticketBean.getUnit();//获取工作票开票单位
            String createByUnit = ticketBean.getDeptId();//获取创建人单位

            List<String> ticketUnitAcclist = ticketpowerservice.getAccount(ticketUnit);//开票单位所有账号
            List<String> createByUnitAcclist = ticketpowerservice.getAccount(createByUnit);//创建人单位所有账号
            Map<String, List<String>> tem = new HashMap();
            tem.put("ticketUnitAcclist", ticketUnitAcclist);
            tem.put("createByUnitAcclist", createByUnitAcclist);
            return tem;
        }, Map.class);
        return Response.ok(map);
    }

    /**
     * 远程调用 获取人员权限
     *
     * @param postUserQuery
     * @return
     */
    private List<PostUserVo> getPostUserVos(PostUserQuery postUserQuery) {
        Response<List<PostUserVo>> response = systemfeign.postqueryList(postUserQuery);
        Assert.isFalse(response.getCode() != 200, StrUtil.format("{}:{}", "人员获取岗位异常", response.getDescription()));
        if (response.getCode() == 200) {
            log.info("post查询结果：{}",response.getData().toString());
            return response.getData();
        }
        return new ArrayList<>();
    }


}
